<?php
/**
 * 登录
 * @author Yangdong Zhang <zhangyangdong@gouuse.cn>
 * @time 2018/10/12 17:16
 */
namespace App\Controller;

use App\Model\User;
use Firebase\JWT\JWT;

class Site extends Base
{
    /**
     * 登录
     * @throws \ErrorException
     * @throws \Exception
     */
    public function login()
    {
        if ($this->isPost()) {
            $data  = $_POST;

            $model = new User();
            $info  = $model->where(['username' => $data['username']])->find();
            if (!$info){
                exit(json_encode($this->error('帐号或者密码错误'), JSON_UNESCAPED_UNICODE));
            }
            $info = $info->toArray();
            if ($info['password'] != md5($data['password'])) {
                exit(json_encode($this->error('帐号或者密码错误'), JSON_UNESCAPED_UNICODE));
            }

            $jwtConfig = getConfig('jwt');
            $token = JWT::encode([
                "iss"     => SITE_URL,
                "aud"     => SITE_URL,
                "iat"     => $_SERVER['REQUEST_TIME'],
                "exp"     => $_SERVER['REQUEST_TIME'] + $jwtConfig['expire'],
                "user_id" => $info['id'],
            ], $jwtConfig['key']);

            exit(json_encode($this->success('登录成功', ['token' => $token]), JSON_UNESCAPED_UNICODE));
        }else {
            $this->display('login');
        }
    }

    /**
     * 注册
     * @throws \ErrorException
     */
    public function register()
    {
        if ($this->isPost()) {
            $data  = $_POST;
            $data['created_at'] = time();
            $data['updated_at'] = time();
            $data['password']   = md5($data['password']);
            $model = new User();
            $model->data($data);
            $id = $model->save();

            if ($id) {
                $jwtConfig = getConfig('jwt');
                $token = JWT::encode([
                    "iss"     => SITE_URL,
                    "aud"     => SITE_URL,
                    "iat"     => $_SERVER['REQUEST_TIME'],
                    "exp"     => $_SERVER['REQUEST_TIME'] + $jwtConfig['expire'],
                    "user_id" => $model->id,
                ], $jwtConfig['key']);
                exit(json_encode($this->success('注册成功', ['token' => $token]), JSON_UNESCAPED_UNICODE));
            }
            exit(json_encode($this->error('注册失败'), JSON_UNESCAPED_UNICODE));
        }else {
            $this->display('register');
        }
    }


    public function test()
    {

        $string = '{"meeting_title":"测试会议室审批数据。","meeting_content":"","meeting_time_day":1540224000,"meeting_time_h_i":"[\"1540285200,1540288800\"]","meeting_person":"[\"100000077\"]","minutes_person":"","attend_way":"[{\"value\":\"1\",\"option\":\"现场会议\"}]","meeting_room":"[{\"value\":3138,\"option\":\"测试会议室1\",\"address\":{\"actual_area_id\":\"\",\"actual_area_name\":\"\",\"province\":\"510000\",\"address\":\"建设北路万科华茂广场\",\"city\":\"510100\",\"area\":\"510108\",\"province_name\":\"四川省\",\"city_name\":\"成都市\",\"area_name\":\"成华区\"}}]","remind":"[{\"value\":4,\"option\":\"提前15分钟\"}]","link_files":"","meeting_service":"[{\"value\":0,\"option\":\"关闭\"}]","service_person":"","service_content":""}';


        $data = json_decode($string, true);

        echo "<pre>";
        foreach ($data as $k=>$v) {
            echo $k.':'.$v."<br>";
        }

        var_dump("<pre>", json_decode($string, true));
        die;

        $string = '{"meeting_title":"测试","meeting_content":"","meeting_time_day":1540137600,"meeting_time_h_i":"[\"1540209600,1540213200\"]","meeting_person":"[\"100058248\"]","minutes_person":"","attend_way":"[{\"value\":\"1\",\"option\":\"现场会议\"}]","meeting_room":"[{\"img_id\":\"13953\",\"value\":3145,\"option\":\"会议室1\",\"address\":{\"actual_area_id\":28,\"actual_area_name\":\"地区名称\",\"province\":\"510000\",\"address\":\"华茂广场\",\"city\":\"510100\",\"area\":\"510108\",\"province_name\":\"四川省\",\"city_name\":\"成都市\",\"area_name\":\"成华区\"}}]","remind":"[{\"value\":4,\"option\":\"提前15分钟\"}]","link_files":"","meeting_service":"[{\"value\":0,\"option\":\"关闭\"}]","service_person":"[\"100058254\",\"100058277\"]","service_content":""}';

        $data = '[{"img_id":"13953","value":3145,"option":"会议室1","address":{"actual_area_id":28,"actual_area_name":"地区名称","province":"510000","address":"华茂广场","city":"510100","area":"510108","province_name":"四川省","city_name":"成都市","area_name":"成华区"}}]';
        var_dump("<pre>", json_decode($string, true), json_decode($data, true));
        die;

        /*$key = "example_key";
        $token = array(
            "iss" => "http://example.org",
            "aud" => "http://example.com",
            "iat" => 1356999524,
            "nbf" => 1357000000,
            'user'  => ['id' => 1, 'name' => '央东', 'email' => '123@qq.com'],
        );

        $jwt = JWT::encode($token, $key);
        echo $jwt;
        die;*/
        $jwt = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9ub3RlLmNvbVwvaW5kZXgucGhwIiwiYXVkIjoiaHR0cDpcL1wvbm90ZS5jb21cL2luZGV4LnBocCIsImlhdCI6MTU0MDE5NzU4OSwiZXhwIjoxNTQwODAyMzg5LCJ1c2VyX2lkIjp0cnVlfQ.Fo9yFknikJaE-gxdHvZLyoyXpuX7m9xKDhVSz0nn42c';
        $decoded = JWT::decode($jwt, 'mark_down_editor', array('HS256'));

        var_dump("<pre>", $decoded);
        die;


        var_dump("<pre>", $jwt, $decoded);

        /*$model  = new User();

        var_dump("<pre>", $model);
        die;*/
    }

}